import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/ui/medicine/take_medicine/add_take_medicine_page.dart';
import 'package:ls_work/util/ext_utils.dart';
import 'package:ls_work/viewmodel/medecine/take_medicine/take_medicine_list_view_model.dart';

import '../../../res/colors.dart';
import '../../../res/gaps.dart';
import '../../../widgets/common_widget.dart';
class TakeMedicineListPage extends BaseStatefulMvvmWidget{
  @override
  State<StatefulWidget> createState() => _TakeMedicineListState();

}

class _TakeMedicineListState extends BaseMvvmListState<TakeMedicineListViewModel,TakeMedicineListPage>{
  GlobalKey<ScaffoldState> key = GlobalKey();

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      key: key,
      backgroundColor: Colours.bg_color,
      appBar: AppBar(
        title: Text('服药清单'),
        actions: [IconButton(onPressed: (){
          key.currentState?.openEndDrawer();
        }, icon: Icon(Icons.search))],
      ),
      body: Column(
        children: [
          loadingContentWidget.setWeight(1),
          SizedBox(height: 12,),
          TextButton(onPressed: () async {
            var data = await Go().push(AddTakeMedicinePage());
            if(data!=null){
              viewModel.requestRefresh();
            }
          }, child: CommonText('新增服药',textColor: Colors.white),
            style: ButtonStyle(
                backgroundColor: WidgetStateProperty.all(Colors.blue,),
                shape: WidgetStateProperty.all(StadiumBorder()),
                minimumSize: WidgetStateProperty.all(Size(MediaQuery.of(context).size.width-32,45))
            ),),
          SizedBox(height: 20,)
        ],
      ),
      endDrawer: endDraw(),
    );
  }

  Widget endDraw() {
    return Column(
      children: [
        AppBar(
          leading: Container(),
          actions: [Container()],
          title: Text('筛选'),
        ),
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(
              height: 12,
            ),
            CommonText('长者姓名'),
            SizedBox(
              height: 8,
            ),
            NoBorderInput(
                hintText: '输入长者姓名',
                text: vm?.searchName ?? "",
                maxLines: 1,
                textInputAction: TextInputAction.search,
                onChanged: (str) {
                  viewModel.searchName = str;
                },
                onSubmitted: (str) {
                  viewModel.requestRefresh();
                })
                .addToContainer(
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)),

            SizedBox(
              height: 12,
            ),
            CommonText('房间号'),
            SizedBox(
              height: 8,
            ),
            NoBorderInput(
                hintText: '输入房间号',
                text: vm?.roomName ?? "",
                maxLines: 1,
                textInputAction: TextInputAction.search,
                onChanged: (str) {
                  viewModel.roomName = str;
                },
                onSubmitted: (str) {
                  viewModel.requestRefresh();
                })
                .addToContainer(
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)),

            SizedBox(height: 10,),

            CommonText('药品名称'),
            SizedBox(
              height: 8,
            ),
            NoBorderInput(
                hintText: '输入药品名称',
                text: vm?.medicineName ?? "",
                maxLines: 1,
                textInputAction: TextInputAction.search,
                onChanged: (str) {
                  viewModel.medicineName = str;
                },
                onSubmitted: (str) {
                  viewModel.requestRefresh();
                })
                .addToContainer(
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)),

            SizedBox(height: 10,),

            CommonText('楼栋'),
            SizedBox(
              height: 8,
            ),
            CommonText(vm?.selectedBuilding?.pickName,hintText: '请选择')
                .addToContainer(
                width: double.infinity,
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)).onTap(() {
              viewModel.chooseBuildingFloor();
            }),

            SizedBox(height: 10,),

            CommonText('楼层'),
            SizedBox(
              height: 8,
            ),
            CommonText(vm?.selectedFloor?.pickName,hintText: '请选择')
                .addToContainer(
                width: double.infinity,
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)).onTap(() {
              viewModel.chooseBuildingFloor();
            }),

            SizedBox(height: 10,),

            CommonText('服药日期'),
            SizedBox(
              height: 8,
            ),
            CommonText(vm?.takeMedicineDate,hintText: '请选择')
                .addToContainer(
                width: double.infinity,
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)).onTap(() {
              viewModel.chooseTakeDate();
            }),

            SizedBox(height: 10,),

            CommonText('服药时间'),
            SizedBox(
              height: 8,
            ),
            CommonText(vm?.takeTime?.pickName,hintText: '请选择')
                .addToContainer(
                width: double.infinity,
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)).onTap(() {
              viewModel.chooseTakeTime();
            }),

            SizedBox(height: 10,),

            CommonText('状态'),
            SizedBox(
              height: 8,
            ),
            CommonText(vm?.selectedStatus?.pickName??"",hintText: '请选择')
                .addToContainer(
                width: double.infinity,
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)).onTap(() {
              viewModel.chooseStatus();
            }),
            SizedBox(height: 10,),
          ],
        ).addToContainer(
          padding: EdgeInsets.symmetric(horizontal: 16),
        ),
        SizedBox().setWeight(1),
        Gaps.dividerLine,
        SizedBox(height: 10,),
        Row(
          children: [
            SizedBox(width: 16,),
            TextButton(
              onPressed: () {
                key.currentState?.closeEndDrawer();
                viewModel.resetClick();
              },
              child: CommonText('重置',textColor: Colors.blue),
              style: ButtonStyle(
                  shape: WidgetStateProperty.all(RoundedRectangleBorder(
                      borderRadius: BorderRadius.horizontal(left: 20.radius),
                      side: BorderSide(color: Colors.blue)
                  ))
              ),)
                .setWeight(1),
            TextButton(
                onPressed: () {
                  key.currentState?.closeEndDrawer();
                  viewModel.requestRefresh();
                },
                child: CommonText('确定',textColor: Colors.white),
                style: ButtonStyle(
                    backgroundColor: WidgetStateProperty.all(Colors.blue),
                    shape: WidgetStateProperty.all(RoundedRectangleBorder(
                      borderRadius: BorderRadius.horizontal(right: 20.radius),
                      side: BorderSide(color: Colors.blue),
                    ))
                ))
                .setWeight(1),
            SizedBox(width: 16,),
          ],
        ),
        SizedBox(height: 12,)
      ],
    ).addToContainer(
        width: MediaQuery.of(context).size.width * 0.7,
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.horizontal(left: 12.radius)));
  }

  @override
  Widget createItemWidget(int index) {
    var item = viewModel.listItems[index];
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        ...[
          Row(
            children: [
              CommonText('${(item.planTime??"").changeTime(format: [yyyy,'-',mm,'-',dd])}',fontWeight: FontWeight.bold).setWeight(1),
              CommonText("${item.customerName??""}",textSize: 17,fontWeight: FontWeight.bold),
              SizedBox(width: 10,),
              CommonText('${item.bedNo??""}')
            ],
          ),
          CommonText('${item.drugName??""}',fontWeight: FontWeight.bold,textSize: 18,textColor: Colors.blue),
          Row(
            children: [
              CommonText('药品规格：${item.specifications??""}').setWeight(3),
              CommonText('用法：${item.usageMethod??""}').setWeight(2),
            ],
          ),

          Row(
            children: [
              CommonText('最小单位（配药单位）：${item.minUnitName??""}').setWeight(3),
              Row(
                children: [
                  CommonText('用量：'),
                  CommonText('${item.usageQuantity?.d}',textColor: Colors.blue,fontWeight: FontWeight.bold)
                ],
              ).setWeight(2),

            ],
          ),

          Row(
            children: [
              Row(
                children: [
                  CommonText('服药时间：'),
                  CommonText('${item.usageTime??""}',textColor: Colors.blue,fontWeight: FontWeight.bold)
                ],
              ).setWeight(3),
              Row(
                children: [
                  CommonText('状态：'),
                  CommonText('${item.statusName??""}',textColor: Colors.blue,fontWeight: FontWeight.bold)
                ],
              ).setWeight(2),

            ],
          ),
        ].insertWidget(SizedBox(height: 10,)),

        SizedBox(height: 12,),

        Row(
          children: [
            CommonText('作废',textColor: item.status==2||item.status==3?Colors.grey:Colors.red).addToContainer(alignment: Alignment.center).addToContainer(
                padding: EdgeInsets.symmetric(vertical: 8)
            ).onTap((){
              if(item.status!=2&&item.status!=3){
                viewModel.cancelTakeMedicine(item.id);
              }
            }).setWeight(1),
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                CommonText(item.assignorName.isNullOrEmpty()?'服药':"已服药",textColor:item.status==1? Colors.blue:Colors.grey),
                if(!item.assignorName.isNullOrEmpty())
                  CommonText('发药人:${item.assignorName}',textColor: Colors.grey,textSize: 14),
                if(!item.assignorTime.isNullOrEmpty())
                  CommonText('${item.assignorTime}',textColor: Colors.grey,textSize: 14),
              ].insertWidget(SizedBox(height: 5,)),
            ).onTap((){
              if(item.status==1){
                viewModel.showApplyTakeDialog(1,item);
              }
            }).addToContainer(padding: EdgeInsets.symmetric(vertical: 8)).setWeight(2),

            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                CommonText((item.verifierName.isNullOrEmpty())?'核对':'已核对',textColor: item.status==0?Colors.blue:Colors.grey),
                if(!item.verifierName.isNullOrEmpty())
                  CommonText('核对人:${item.verifierName}',textColor: Colors.grey,textSize: 14),
                if(!item.verifierTime.isNullOrEmpty())
                  CommonText('${item.verifierTime}',textColor: Colors.grey,textSize: 14),
              ].insertWidget(SizedBox(height: 5,)),
            ).onTap((){
              if(item.status==0){
                viewModel.showApplyTakeDialog(0,item);
              }
            }).addToContainer(padding: EdgeInsets.symmetric(vertical: 8)).setWeight(2),
          ].insertWidget(Container(width: 0.5,color: Colors.grey,height: 35,)),
        ).addToContainer(
            decoration: BoxDecoration(
                border: Border.all(color: Colors.grey,width: 0.5)
            )
        )
      ],
    ).addToContainer(
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: 6.borderRadius
        ),
        padding: EdgeInsets.symmetric(horizontal: 10,vertical: 10)
    );
  }
  
  @override
  TakeMedicineListViewModel createViewModel() {
    return TakeMedicineListViewModel(context);
  }

  @override
  EdgeInsets get listPadding => EdgeInsets.symmetric(horizontal: 16,vertical: 12);

  @override
  Widget get separatorDivider => SizedBox(height: 12,);

  @override
  bool get canPullUp => true;

}